<template>
  <el-dialog
    :title="!dataForm.recordid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="司机账号" prop="driverid">
      <el-input v-model="dataForm.driverid" placeholder="司机账号"></el-input>
    </el-form-item>
    <el-form-item label="助手账号" prop="datcherid">
      <el-input v-model="dataForm.datcherid" placeholder="助手账号"></el-input>
    </el-form-item>
    <el-form-item label="驻厂人员账号" prop="reid">
      <el-input v-model="dataForm.reid" placeholder="驻厂人员账号"></el-input>
    </el-form-item>
    <el-form-item label="车牌号" prop="carnumber">
      <el-input v-model="dataForm.carnumber" placeholder="车牌号"></el-input>
    </el-form-item>
    <el-form-item label="报道时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="报道时间"></el-input>
    </el-form-item>
    <el-form-item label="报道状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="报道状态"></el-input>
    </el-form-item>
    <el-form-item label="承运路线" prop="way">
      <el-input v-model="dataForm.way" placeholder="承运路线"></el-input>
    </el-form-item>
    <el-form-item label="运载料品" prop="carrythings">
      <el-input v-model="dataForm.carrythings" placeholder="运载料品"></el-input>
    </el-form-item>
    <el-form-item label="吨数" prop="tonnage">
      <el-input v-model="dataForm.tonnage" placeholder="吨数"></el-input>
    </el-form-item>
    <el-form-item label="进货厂区" prop="purchaseplant">
      <el-input v-model="dataForm.purchaseplant" placeholder="进货厂区"></el-input>
    </el-form-item>
    <el-form-item label="排放标准" prop="emissionstandard">
      <el-input v-model="dataForm.emissionstandard" placeholder="排放标准"></el-input>
    </el-form-item>
    <el-form-item label="所属车商" prop="carshop">
      <el-input v-model="dataForm.carshop" placeholder="所属车商"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordid: 0,
          driverid: '',
          datcherid: '',
          reid: '',
          carnumber: '',
          time: '',
          state: '',
          way: '',
          carrythings: '',
          tonnage: '',
          purchaseplant: '',
          emissionstandard: '',
          carshop: ''
        },
        dataRule: {
          driverid: [
            { required: true, message: '司机账号不能为空', trigger: 'blur' }
          ],
          datcherid: [
            { required: true, message: '助手账号不能为空', trigger: 'blur' }
          ],
          reid: [
            { required: true, message: '驻厂人员账号不能为空', trigger: 'blur' }
          ],
          carnumber: [
            { required: true, message: '车牌号不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '报道时间不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '报道状态不能为空', trigger: 'blur' }
          ],
          way: [
            { required: true, message: '承运路线不能为空', trigger: 'blur' }
          ],
          carrythings: [
            { required: true, message: '运载料品不能为空', trigger: 'blur' }
          ],
          tonnage: [
            { required: true, message: '吨数不能为空', trigger: 'blur' }
          ],
          purchaseplant: [
            { required: true, message: '进货厂区不能为空', trigger: 'blur' }
          ],
          emissionstandard: [
            { required: true, message: '排放标准不能为空', trigger: 'blur' }
          ],
          carshop: [
            { required: true, message: '所属车商不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/registration/info/${this.dataForm.recordid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.driverid = data.registration.driverid
                this.dataForm.datcherid = data.registration.datcherid
                this.dataForm.reid = data.registration.reid
                this.dataForm.carnumber = data.registration.carnumber
                this.dataForm.time = data.registration.time
                this.dataForm.state = data.registration.state
                this.dataForm.way = data.registration.way
                this.dataForm.carrythings = data.registration.carrythings
                this.dataForm.tonnage = data.registration.tonnage
                this.dataForm.purchaseplant = data.registration.purchaseplant
                this.dataForm.emissionstandard = data.registration.emissionstandard
                this.dataForm.carshop = data.registration.carshop
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/registration/${!this.dataForm.recordid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordid': this.dataForm.recordid || undefined,
                'driverid': this.dataForm.driverid,
                'datcherid': this.dataForm.datcherid,
                'reid': this.dataForm.reid,
                'carnumber': this.dataForm.carnumber,
                'time': this.dataForm.time,
                'state': this.dataForm.state,
                'way': this.dataForm.way,
                'carrythings': this.dataForm.carrythings,
                'tonnage': this.dataForm.tonnage,
                'purchaseplant': this.dataForm.purchaseplant,
                'emissionstandard': this.dataForm.emissionstandard,
                'carshop': this.dataForm.carshop
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
